#include "memberrechargepage.h"
#include <QApplication>
#include <QHeaderView>
#include <QSplitter>
#include <QScrollArea>
#include <QDebug>

MemberRechargePage::MemberRechargePage(Database *db, QWidget *parent)
    : QWidget(parent)
    , m_db(db)
    , m_memberService(nullptr)
    , m_mainLayout(nullptr)
    , m_memberSelectionGroup(nullptr)
    , m_searchEdit(nullptr)
    , m_searchButton(nullptr)
    , m_memberTable(nullptr)
    , m_rechargeInfoGroup(nullptr)
    , m_rechargeAmountSpinBox(nullptr)
    , m_remarkEdit(nullptr)
    , m_memberInfoGroup(nullptr)
    , m_memberNameLabel(nullptr)
    , m_memberPhoneLabel(nullptr)
    , m_currentLevelLabel(nullptr)
    , m_currentBalanceLabel(nullptr)
    , m_totalRechargedLabel(nullptr)
    , m_predictedLevelLabel(nullptr)
    , m_rechargeButton(nullptr)
    , m_resetButton(nullptr)
    , m_currentMemberId(-1)
{
    // 初始化服务层
    m_memberService = new MemberService(m_db);
    
    // 初始化界面
    initializeUI();
    
    // 设置样式
    setUIStyles();
    
    // 加载会员列表
    loadMemberList();
}

MemberRechargePage::~MemberRechargePage()
{
    delete m_memberService;
}

void MemberRechargePage::initializeUI()
{
    // 创建主布局
    m_mainLayout = new QVBoxLayout(this);
    m_mainLayout->setSpacing(20);
    m_mainLayout->setContentsMargins(25, 25, 25, 25);
    
    // 创建标题
    QLabel *titleLabel = new QLabel(tr("会员充值管理"));
    titleLabel->setObjectName("titleLabel");
    titleLabel->setAlignment(Qt::AlignCenter);
    m_mainLayout->addWidget(titleLabel);
    
    // 创建主要内容区域
    QWidget *contentWidget = new QWidget();
    QHBoxLayout *contentLayout = new QHBoxLayout(contentWidget);
    contentLayout->setSpacing(25);
    contentLayout->setContentsMargins(0, 0, 0, 0);
    
    // 左侧区域：会员搜索选择（优化宽度）
    QWidget *leftWidget = new QWidget();
    leftWidget->setMinimumWidth(500);
    leftWidget->setMaximumWidth(600);
    QVBoxLayout *leftLayout = new QVBoxLayout(leftWidget);
    leftLayout->setSpacing(15);
    leftLayout->setContentsMargins(0, 0, 0, 0);
    leftLayout->addWidget(createMemberSelectionGroup());
    leftLayout->addStretch();
    
    // 右侧区域：充值操作和会员信息
    QWidget *rightWidget = new QWidget();
    QVBoxLayout *rightLayout = new QVBoxLayout(rightWidget);
    rightLayout->setSpacing(15);
    rightLayout->setContentsMargins(0, 0, 0, 0);
    rightLayout->addWidget(createRechargeInfoGroup());
    rightLayout->addWidget(createMemberInfoGroup());
    rightLayout->addWidget(createButtonArea());
    rightLayout->addStretch();
    
    contentLayout->addWidget(leftWidget);
    contentLayout->addWidget(rightWidget);
    
    // 设置布局比例：左侧搜索区域 : 右侧操作区域 = 3:2
    contentLayout->setStretchFactor(leftWidget, 3);
    contentLayout->setStretchFactor(rightWidget, 2);
    
    m_mainLayout->addWidget(contentWidget);
}

QGroupBox* MemberRechargePage::createMemberSelectionGroup()
{
    m_memberSelectionGroup = new QGroupBox(tr("会员搜索与选择"));
    QVBoxLayout *layout = new QVBoxLayout(m_memberSelectionGroup);
    
    // 搜索区域
    QHBoxLayout *searchLayout = new QHBoxLayout();
    QLabel *searchLabel = new QLabel(tr("搜索会员:"));
    m_searchEdit = new QLineEdit();
    m_searchEdit->setPlaceholderText(tr("输入会员姓名或手机号进行搜索"));
    m_searchButton = new QPushButton(tr("搜索"));
    m_searchButton->setObjectName("searchButton");
    
    searchLayout->addWidget(searchLabel);
    searchLayout->addWidget(m_searchEdit, 1);
    searchLayout->addWidget(m_searchButton);
    
    // 会员列表表格
    m_memberTable = new QTableWidget();
    m_memberTable->setColumnCount(4);
    QStringList headers;
    headers << tr("会员姓名") << tr("手机号码") << tr("当前等级") << tr("账户余额");
    m_memberTable->setHorizontalHeaderLabels(headers);
    
    // 设置表格属性
    m_memberTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_memberTable->setSelectionMode(QAbstractItemView::SingleSelection);
    m_memberTable->setAlternatingRowColors(true);
    m_memberTable->setSortingEnabled(true);
    m_memberTable->setMinimumHeight(200);
    m_memberTable->setMaximumHeight(300);
    
    // 设置列宽
    m_memberTable->horizontalHeader()->setStretchLastSection(true);
    m_memberTable->setColumnWidth(0, 120);
    m_memberTable->setColumnWidth(1, 130);
    m_memberTable->setColumnWidth(2, 100);
    
    layout->addLayout(searchLayout);
    layout->addWidget(m_memberTable);
    
    // 连接信号槽
    connect(m_searchButton, &QPushButton::clicked, this, &MemberRechargePage::onSearchMember);
    connect(m_memberTable, &QTableWidget::itemSelectionChanged,
            this, &MemberRechargePage::onMemberSelectionChanged);
    connect(m_searchEdit, &QLineEdit::returnPressed, this, &MemberRechargePage::onSearchMember);
    connect(m_searchEdit, &QLineEdit::textChanged, this, &MemberRechargePage::onSearchMember);
    
    return m_memberSelectionGroup;
}

QGroupBox* MemberRechargePage::createRechargeInfoGroup()
{
    m_rechargeInfoGroup = new QGroupBox(tr("充值信息"));
    QGridLayout *layout = new QGridLayout(m_rechargeInfoGroup);
    
    // 充值金额
    QLabel *amountLabel = new QLabel(tr("充值金额:"));
    m_rechargeAmountSpinBox = new QDoubleSpinBox();
    m_rechargeAmountSpinBox->setRange(0.01, 99999.99);
    m_rechargeAmountSpinBox->setDecimals(2);
    m_rechargeAmountSpinBox->setSuffix(tr(" 元"));
    m_rechargeAmountSpinBox->setValue(100.00);
    
    // 备注信息
    QLabel *remarkLabel = new QLabel(tr("备注信息:"));
    m_remarkEdit = new QLineEdit();
    m_remarkEdit->setPlaceholderText(tr("可选：充值备注信息"));
    
    layout->addWidget(amountLabel, 0, 0);
    layout->addWidget(m_rechargeAmountSpinBox, 0, 1);
    layout->addWidget(remarkLabel, 1, 0);
    layout->addWidget(m_remarkEdit, 1, 1);
    
    // 连接信号槽
    connect(m_rechargeAmountSpinBox, QOverload<double>::of(&QDoubleSpinBox::valueChanged),
            this, &MemberRechargePage::onRechargeAmountChanged);
    
    return m_rechargeInfoGroup;
}

QWidget* MemberRechargePage::createButtonArea()
{
    QWidget *buttonWidget = new QWidget();
    QHBoxLayout *layout = new QHBoxLayout(buttonWidget);
    
    m_rechargeButton = new QPushButton(tr("确认充值"));
    m_rechargeButton->setObjectName("primaryButton");
    m_rechargeButton->setEnabled(false);
    
    m_resetButton = new QPushButton(tr("重置表单"));
    m_resetButton->setObjectName("secondaryButton");
    
    // 移除充值历史按钮
    
    layout->addWidget(m_rechargeButton);
    layout->addWidget(m_resetButton);
    layout->addStretch();
    
    // 连接信号槽
    connect(m_rechargeButton, &QPushButton::clicked, this, &MemberRechargePage::onRechargeButtonClicked);
    connect(m_resetButton, &QPushButton::clicked, this, &MemberRechargePage::onResetButtonClicked);
    
    return buttonWidget;
}

QGroupBox* MemberRechargePage::createMemberInfoGroup()
{
    m_memberInfoGroup = new QGroupBox(tr("会员信息"));
    QGridLayout *layout = new QGridLayout(m_memberInfoGroup);
    
    // 创建信息标签
    QLabel *nameLabel = new QLabel(tr("会员姓名:"));
    m_memberNameLabel = new QLabel(tr("未选择"));
    m_memberNameLabel->setObjectName("infoValue");
    
    QLabel *phoneLabel = new QLabel(tr("手机号码:"));
    m_memberPhoneLabel = new QLabel(tr("未选择"));
    m_memberPhoneLabel->setObjectName("infoValue");
    
    QLabel *levelLabel = new QLabel(tr("当前等级:"));
    m_currentLevelLabel = new QLabel(tr("未选择"));
    m_currentLevelLabel->setObjectName("infoValue");
    
    QLabel *balanceLabel = new QLabel(tr("账户余额:"));
    m_currentBalanceLabel = new QLabel(tr("未选择"));
    m_currentBalanceLabel->setObjectName("infoValue");
    
    QLabel *totalLabel = new QLabel(tr("累计充值:"));
    m_totalRechargedLabel = new QLabel(tr("未选择"));
    m_totalRechargedLabel->setObjectName("infoValue");
    
    QLabel *predictLabel = new QLabel(tr("充值后等级:"));
    m_predictedLevelLabel = new QLabel(tr("未选择"));
    m_predictedLevelLabel->setObjectName("predictValue");
    
    // 添加到布局
    layout->addWidget(nameLabel, 0, 0);
    layout->addWidget(m_memberNameLabel, 0, 1);
    layout->addWidget(phoneLabel, 1, 0);
    layout->addWidget(m_memberPhoneLabel, 1, 1);
    layout->addWidget(levelLabel, 2, 0);
    layout->addWidget(m_currentLevelLabel, 2, 1);
    layout->addWidget(balanceLabel, 3, 0);
    layout->addWidget(m_currentBalanceLabel, 3, 1);
    layout->addWidget(totalLabel, 4, 0);
    layout->addWidget(m_totalRechargedLabel, 4, 1);
    
    // 添加分隔线
    QFrame *line = new QFrame();
    line->setFrameShape(QFrame::HLine);
    line->setFrameShadow(QFrame::Sunken);
    layout->addWidget(line, 5, 0, 1, 2);
    
    layout->addWidget(predictLabel, 6, 0);
    layout->addWidget(m_predictedLevelLabel, 6, 1);
    
    return m_memberInfoGroup;
}

void MemberRechargePage::loadMemberList()
{
    // 获取所有会员列表
    m_memberList = m_memberService->getAllMembersForDisplay();
    
    // 添加调试信息
    qDebug() << "加载会员列表，共" << m_memberList.size() << "条记录";
    if (!m_memberList.isEmpty()) {
        qDebug() << "第一条记录字段:" << m_memberList.first().keys();
        qDebug() << "第一条记录内容:" << m_memberList.first();
    }
    
    // 填充表格
    populateMemberTable(m_memberList);
}

void MemberRechargePage::populateMemberTable(const QVector<QMap<QString, QVariant>> &memberList)
{
    m_memberTable->setRowCount(memberList.size());
    
    for (int i = 0; i < memberList.size(); ++i) {
        const auto &member = memberList[i];
        
        // 会员姓名
        QTableWidgetItem *nameItem = new QTableWidgetItem(member["name"].toString());
        nameItem->setData(Qt::UserRole, member["customer_id"].toInt());
        m_memberTable->setItem(i, 0, nameItem);
        
        // 手机号码
        QTableWidgetItem *phoneItem = new QTableWidgetItem(member["phone"].toString());
        m_memberTable->setItem(i, 1, phoneItem);
        
        // 当前等级
        QTableWidgetItem *levelItem = new QTableWidgetItem(member["level_name"].toString());
        m_memberTable->setItem(i, 2, levelItem);
        
        // 账户余额
        QTableWidgetItem *balanceItem = new QTableWidgetItem(member["balance"].toString());
        m_memberTable->setItem(i, 3, balanceItem);
        
        // 设置行不可编辑
        for (int j = 0; j < 4; ++j) {
            m_memberTable->item(i, j)->setFlags(m_memberTable->item(i, j)->flags() & ~Qt::ItemIsEditable);
        }
    }
    
    // 调整行高
    m_memberTable->resizeRowsToContents();
}

void MemberRechargePage::onMemberSelectionChanged()
{
    QList<QTableWidgetItem*> selectedItems = m_memberTable->selectedItems();
    if (selectedItems.isEmpty()) {
        // 未选择会员
        m_currentMemberId = -1;
        m_rechargeButton->setEnabled(false);
        
        // 重置会员信息显示
        m_memberNameLabel->setText(tr("未选择"));
        m_memberPhoneLabel->setText(tr("未选择"));
        m_currentLevelLabel->setText(tr("未选择"));
        m_currentBalanceLabel->setText(tr("未选择"));
        m_totalRechargedLabel->setText(tr("未选择"));
        m_predictedLevelLabel->setText(tr("未选择"));
        return;
    }
    
    // 获取选中行的第一列（姓名列），从中获取会员ID
    int selectedRow = m_memberTable->currentRow();
    if (selectedRow < 0) {
        m_currentMemberId = -1;
        m_rechargeButton->setEnabled(false);
        return;
    }
    
    QTableWidgetItem *nameItem = m_memberTable->item(selectedRow, 0);
    if (nameItem) {
        int memberId = nameItem->data(Qt::UserRole).toInt();
        m_currentMemberId = memberId;
        
        // 更新会员信息显示
        updateMemberInfo(m_currentMemberId);
        calculateLevelChange();
        
        m_rechargeButton->setEnabled(true);
    }
}

void MemberRechargePage::updateMemberInfo(int memberId)
{
    auto memberInfo = m_memberService->getFormattedMemberInfo(memberId);
    
    if (memberInfo.isEmpty()) {
        QMessageBox::warning(this, tr("错误"), tr("无法获取会员信息"));
        return;
    }
    
    m_memberNameLabel->setText(memberInfo["customer_name"].toString());
    m_memberPhoneLabel->setText(memberInfo["customer_phone"].toString());
    m_currentLevelLabel->setText(memberInfo["level_name"].toString());
    m_currentBalanceLabel->setText(m_memberService->formatAmount(memberInfo["balance"].toDouble()));
    m_totalRechargedLabel->setText(m_memberService->formatAmount(memberInfo["total_recharged"].toDouble()));
}

void MemberRechargePage::onRechargeAmountChanged()
{
    if (m_currentMemberId > 0) {
        calculateLevelChange();
    }
}

void MemberRechargePage::calculateLevelChange()
{
    if (m_currentMemberId <= 0) {
        m_predictedLevelLabel->setText(tr("未选择"));
        return;
    }
    
    auto memberInfo = m_memberService->getFormattedMemberInfo(m_currentMemberId);
    if (memberInfo.isEmpty()) {
        return;
    }
    
    double currentTotal = memberInfo["total_recharged"].toDouble();
    double rechargeAmount = m_rechargeAmountSpinBox->value();
    double newTotal = currentTotal + rechargeAmount;
    
    // 根据新的累计充值金额判断等级
    QString newLevel;
    if (newTotal >= 1000.00) {
        newLevel = tr("高级会员");
    } else if (newTotal >= 500.00) {
        newLevel = tr("中级会员");
    } else if (newTotal >= 100.00) {
        newLevel = tr("初级会员");
    } else {
        newLevel = tr("非会员");
    }
    
    QString currentLevel = memberInfo["level_name"].toString();
    if (newLevel != currentLevel) {
        m_predictedLevelLabel->setText(QString("%1 (升级!)").arg(newLevel));
        m_predictedLevelLabel->setStyleSheet("color: #27AE60; font-weight: bold;");
    } else {
        m_predictedLevelLabel->setText(newLevel);
        m_predictedLevelLabel->setStyleSheet("color: #2C3E50;");
    }
}

void MemberRechargePage::onRechargeButtonClicked()
{
    if (!validateRechargeForm()) {
        return;
    }
    
    double amount = m_rechargeAmountSpinBox->value();
    QString remark = m_remarkEdit->text().trimmed();
    
    // 确认对话框
    QString confirmText = tr("确认为会员 %1 充值 %2 元吗？")
                         .arg(m_memberNameLabel->text())
                         .arg(m_memberService->formatAmount(amount));
    
    // 创建自定义消息框，确保按钮字体为深色
    QMessageBox confirmBox(this);
    confirmBox.setWindowTitle(tr("确认充值"));
    confirmBox.setText(confirmText);
    confirmBox.setIcon(QMessageBox::Question);
    confirmBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    confirmBox.setDefaultButton(QMessageBox::No);
    
    // 直接获取按钮并设置样式
    QList<QPushButton*> buttons = confirmBox.findChildren<QPushButton*>();
    for (QPushButton* button : buttons) {
        button->setStyleSheet(
            "QPushButton { "
            "    color: black; "
            "    background-color: #F0F0F0; "
            "    border: 1px solid #CCCCCC; "
            "    padding: 6px 12px; "
            "    border-radius: 4px; "
            "    font-weight: bold; "
            "}"
            "QPushButton:hover { "
            "    background-color: #E0E0E0; "
            "}"
            "QPushButton:pressed { "
            "    background-color: #D0D0D0; "
            "}"
        );
    }
    
    int ret = confirmBox.exec();
    
    if (ret != QMessageBox::Yes) {
        return;
    }
    
    // 获取充值前的会员信息用于比较
    auto beforeInfo = m_memberService->getFormattedMemberInfo(m_currentMemberId);
    QString oldLevel = beforeInfo["level_name"].toString();
    
    // 调用Service层处理充值逻辑，自动更新会员等级
    bool success = m_memberService->processMemberRecharge(m_currentMemberId, amount, remark);
    
    if (success) {
        // 获取充值后的会员信息
        auto afterInfo = m_memberService->getFormattedMemberInfo(m_currentMemberId);
        QString newLevel = afterInfo["level_name"].toString();
        
        QString successMessage;
        if (oldLevel != newLevel) {
            successMessage = tr("充值成功！充值金额：%1 元，会员等级从 %2 升级为 %3")
                           .arg(m_memberService->formatAmount(amount))
                           .arg(oldLevel)
                           .arg(newLevel);
        } else {
            successMessage = tr("充值成功！充值金额：%1 元，当前等级：%2")
                           .arg(m_memberService->formatAmount(amount))
                           .arg(newLevel);
        }
        
        handleRechargeSuccess(successMessage);
    } else {
        handleRechargeError(tr("充值失败，请重试或联系管理员。"));
    }
}

void MemberRechargePage::onResetButtonClicked()
{
    resetForm();
}

// 移除充值历史功能实现

void MemberRechargePage::onSearchMember()
{
    QString searchText = m_searchEdit->text().trimmed();
    if (searchText.isEmpty()) {
        // 显示所有会员
        populateMemberTable(m_memberList);
        return;
    }
    
    // 搜索匹配的会员
    QVector<QMap<QString, QVariant>> filteredList;
    for (const auto &member : m_memberList) {
        QString name = member["name"].toString();
        QString phone = member["phone"].toString();
        
        if (name.contains(searchText, Qt::CaseInsensitive) || 
            phone.contains(searchText, Qt::CaseInsensitive)) {
            filteredList.append(member);
        }
    }
    
    // 更新表格显示
    populateMemberTable(filteredList);
}

bool MemberRechargePage::validateRechargeForm()
{
    if (m_currentMemberId <= 0) {
        QMessageBox::warning(this, tr("错误"), tr("请选择要充值的会员"));
        return false;
    }
    
    if (m_rechargeAmountSpinBox->value() <= 0) {
        QMessageBox::warning(this, tr("错误"), tr("充值金额必须大于0"));
        return false;
    }
    
    return true;
}

void MemberRechargePage::handleRechargeSuccess(const QString &message)
{
    // 创建自定义成功提示框，确保按钮字体为深色
    QMessageBox successBox(this);
    successBox.setWindowTitle(tr("成功"));
    successBox.setText(message);
    successBox.setIcon(QMessageBox::Information);
    successBox.setStandardButtons(QMessageBox::Ok);
    
    // 直接获取按钮并设置样式
    QList<QPushButton*> buttons = successBox.findChildren<QPushButton*>();
    for (QPushButton* button : buttons) {
        button->setStyleSheet(
            "QPushButton { "
            "    color: black; "
            "    background-color: #F0F0F0; "
            "    border: 1px solid #CCCCCC; "
            "    padding: 6px 12px; "
            "    border-radius: 4px; "
            "    font-weight: bold; "
            "}"
            "QPushButton:hover { "
            "    background-color: #E0E0E0; "
            "}"
            "QPushButton:pressed { "
            "    background-color: #D0D0D0; "
            "}"
        );
    }
    
    successBox.exec();
    
    // 刷新会员信息
    updateMemberInfo(m_currentMemberId);
    calculateLevelChange();
    
    // 清空备注
    m_remarkEdit->clear();
    
    // 充值成功后，将页面上的白色字体修改为黑色
    updateTextColorsToBlack();
}

void MemberRechargePage::handleRechargeError(const QString &error)
{
    // 创建自定义错误提示框，确保按钮字体为深色
    QMessageBox errorBox(this);
    errorBox.setWindowTitle(tr("错误"));
    errorBox.setText(error);
    errorBox.setIcon(QMessageBox::Critical);
    errorBox.setStandardButtons(QMessageBox::Ok);
    
    // 直接获取按钮并设置样式
    QList<QPushButton*> buttons = errorBox.findChildren<QPushButton*>();
    for (QPushButton* button : buttons) {
        button->setStyleSheet(
            "QPushButton { "
            "    color: black; "
            "    background-color: #F0F0F0; "
            "    border: 1px solid #CCCCCC; "
            "    padding: 6px 12px; "
            "    border-radius: 4px; "
            "    font-weight: bold; "
            "}"
            "QPushButton:hover { "
            "    background-color: #E0E0E0; "
            "}"
            "QPushButton:pressed { "
            "    background-color: #D0D0D0; "
            "}"
        );
    }
    
    errorBox.exec();
}

void MemberRechargePage::resetForm()
{
    m_rechargeAmountSpinBox->setValue(100.00);
    m_remarkEdit->clear();
    m_searchEdit->clear();
    m_currentMemberId = -1;
    
    // 重置会员信息显示
    m_memberNameLabel->setText(tr("未选择"));
    m_memberPhoneLabel->setText(tr("未选择"));
    m_currentLevelLabel->setText(tr("未选择"));
    m_currentBalanceLabel->setText(tr("未选择"));
    m_totalRechargedLabel->setText(tr("未选择"));
    m_predictedLevelLabel->setText(tr("未选择"));
    
    // 清空表格选择
    m_memberTable->clearSelection();
    
    m_rechargeButton->setEnabled(false);
}

void MemberRechargePage::setUIStyles()
{
    // 设置绿色主题的现代化样式
    setStyleSheet(
        // 整体页面背景
        "MemberRechargePage {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #F8FFF8, stop:1 #E8F5E8);"
        "    color: #1B5E20;"
        "}"
        
        // 确保所有标签文字为深色
        "QLabel {"
        "    color: #1B5E20;"
        "    font-size: 16px;"
        "}"
        
        // 分组框样式 - 绿色主题
        "QGroupBox {"
        "    font-weight: bold;"
        "    font-size: 18px;"
        "    border: 2px solid #A8D5A8;"
        "    border-radius: 12px;"
        "    margin-top: 15px;"
        "    padding-top: 15px;"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #FFFFFF, stop:1 #F0FFF0);"
        "}"
        "QGroupBox::title {"
        "    subcontrol-origin: margin;"
        "    left: 15px;"
        "    padding: 5px 12px;"
        "    color: #1B5E20;"
        "    background: qlineargradient(x1:0, y1:0, x2:1, y2:0, "
        "                                stop:0 #C8E6C9, stop:1 #A5D6A7);"
        "    border-radius: 8px;"
        "    font-weight: bold;"
        "}"
        
        // 标题标签
        "#titleLabel {"
        "    font-size: 32px;"
        "    font-weight: bold;"
        "    color: #1B5E20;"
        "    margin: 15px 0;"
        "    height: 60px;"
        "    background: qlineargradient(x1:0, y1:0, x2:1, y2:0, "
        "                                stop:0 #66BB6A, stop:1 #4CAF50);"
        "    border-radius: 8px;"
        "    padding: 10px;"
        "}"
        
        // 主要按钮 - 绿色渐变
        "#primaryButton {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #66BB6A, stop:1 #4CAF50);"
        "    color: #0D4E14;"
        "    border: none;"
        "    padding: 12px 24px;"
        "    border-radius: 8px;"
        "    font-weight: bold;"
        "    font-size: 16px;"
        "    min-width: 120px;"
        "}"
        "#primaryButton:hover {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #5CB85C, stop:1 #449D44);"
        "}"
        "#primaryButton:pressed {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #449D44, stop:1 #357A35);"
        "}"
        "#primaryButton:disabled {"
        "    background: #C8E6C9;"
        "    color: #81C784;"
        "}"
        
        // 次要按钮 - 浅绿色
        "#secondaryButton {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #C8E6C9, stop:1 #A5D6A7);"
        "    color: #1B5E20;"
        "    border: 2px solid #81C784;"
        "    padding: 12px 24px;"
        "    border-radius: 8px;"
        "    font-weight: bold;"
        "    font-size: 16px;"
        "    min-width: 120px;"
        "}"
        "#secondaryButton:hover {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #A5D6A7, stop:1 #81C784);"
        "    border-color: #66BB6A;"
        "}"
        
        // 搜索按钮 - 深绿色
        "#searchButton {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #388E3C, stop:1 #2E7D32);"
        "    color: #E8F5E8;"
        "    border: none;"
        "    padding: 10px 20px;"
        "    border-radius: 6px;"
        "    font-weight: bold;"
        "    font-size: 16px;"
        "    min-width: 100px;"
        "}"
        "#searchButton:hover {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #2E7D32, stop:1 #1B5E20);"
        "}"
        "#searchButton:pressed {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #1B5E20, stop:1 #0D4E14);"
        "}"
        
        // 表格样式 - 绿色主题
        "QTableWidget {"
        "    border: 2px solid #A5D6A7;"
        "    border-radius: 8px;"
        "    background-color: #FFFFFF;"
        "    gridline-color: #E8F5E8;"
        "    selection-background-color: #C8E6C9;"
        "    selection-color: #1B5E20;"
        "    alternate-background-color: #F1F8E9;"
        "}"
        "QTableWidget::item {"
        "    padding: 10px;"
        "    border-bottom: 1px solid #E8F5E8;"
        "    color: #2E7D32;"
        "    font-size: 15px;"
        "}"
        "QTableWidget::item:selected {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #C8E6C9, stop:1 #A5D6A7);"
        "    color: #1B5E20;"
        "    font-weight: bold;"
        "}"
        "QTableWidget::item:hover {"
        "    background-color: #E8F5E8;"
        "}"
        "QHeaderView::section {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #4CAF50, stop:1 #388E3C);"
        "    color: #0D4E14;"
        "    padding: 12px;"
        "    border: none;"
        "    font-weight: bold;"
        "    font-size: 16px;"
        "    border-right: 1px solid #2E7D32;"
        "}"
        "QHeaderView::section:first {"
        "    border-top-left-radius: 6px;"
        "}"
        "QHeaderView::section:last {"
        "    border-top-right-radius: 6px;"
        "    border-right: none;"
        "}"
        
        // 信息按钮
        "#infoButton {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #81C784, stop:1 #66BB6A);"
        "    color: #0D4E14;"
        "    border: none;"
        "    padding: 10px 20px;"
        "    border-radius: 6px;"
        "    font-weight: bold;"
        "    font-size: 16px;"
        "    min-width: 100px;"
        "}"
        "#infoButton:hover {"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #66BB6A, stop:1 #4CAF50);"
        "}"
        "#infoButton:disabled {"
        "    background: #C8E6C9;"
        "    color: #81C784;"
        "}"
        
        // 信息值显示
        "#infoValue {"
        "    color: #1B5E20;"
        "    font-weight: normal;"
        "    font-size: 16px;"
        "    padding: 8px 12px;"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #F1F8E9, stop:1 #E8F5E8);"
        "    border: 1px solid #C8E6C9;"
        "    border-radius: 6px;"
        "}"
        "#predictValue {"
        "    color: #0D4E14;"
        "    font-weight: bold;"
        "    font-size: 16px;"
        "    padding: 8px 12px;"
        "    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
        "                                stop:0 #E8F5E8, stop:1 #C8E6C9);"
        "    border: 2px solid #81C784;"
        "    border-radius: 6px;"
        "}"
        
        // 输入框样式
        "QLineEdit, QComboBox, QDoubleSpinBox {"
        "    padding: 10px 12px;"
        "    border: 2px solid #C8E6C9;"
        "    border-radius: 6px;"
        "    background-color: #FFFFFF;"
        "    color: #1B5E20;"
        "    font-size: 16px;"
        "    selection-background-color: #A5D6A7;"
        "}"
        "QLineEdit:focus, QComboBox:focus, QDoubleSpinBox:focus {"
        "    border-color: #4CAF50;"
        "    background-color: #F8FFF8;"
        "}"
        "QLineEdit:hover, QComboBox:hover, QDoubleSpinBox:hover {"
        "    border-color: #81C784;"
        "    background-color: #F8FFF8;"
        "}"
        
        // 下拉框箭头
        "QComboBox::drop-down {"
        "    border: none;"
        "    width: 20px;"
        "}"
        "QComboBox::down-arrow {"
        "    image: none;"
        "    border-left: 5px solid transparent;"
        "    border-right: 5px solid transparent;"
        "    border-top: 5px solid #4CAF50;"
        "}"
        
        // 消息框样式
        "QMessageBox {"
        "    background-color: #F8FFF8;"
        "    color: #1B5E20;"
        "}"
        "QMessageBox QLabel {"
        "    color: #1B5E20;"
        "    font-size: 16px;"
        "}"
        
        // 滚动条样式
        "QScrollBar:vertical {"
        "    background: #E8F5E8;"
        "    width: 12px;"
        "    border-radius: 6px;"
        "}"
        "QScrollBar::handle:vertical {"
        "    background: #81C784;"
        "    border-radius: 6px;"
        "    min-height: 20px;"
        "}"
        "QScrollBar::handle:vertical:hover {"
        "    background: #66BB6A;"
        "}"
        "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
        "    height: 0px;"
        "}"
    );
}

void MemberRechargePage::refreshPage()
{
    loadMemberList();
}

void MemberRechargePage::updateTextColorsToBlack()
{
    // 更新样式表，将白色字体修改为黑色
    QString updatedStyleSheet = styleSheet();
    
    // 替换按钮中的白色字体为黑色
    updatedStyleSheet.replace("color: white;", "color: black;");
    
    // 替换表格选中项的白色字体为黑色
    updatedStyleSheet.replace("selection-color: white;", "selection-color: black;");
    
    // 替换表头的白色字体为黑色
    updatedStyleSheet.replace("QHeaderView::section {\n    background-color: #34495E;\n    color: white;", 
                             "QHeaderView::section {\n    background-color: #34495E;\n    color: black;");
    
    // 替换表格选中项的白色字体为黑色
    updatedStyleSheet.replace("QTableWidget::item:selected {\n    background-color: #3498DB;\n    color: white;", 
                             "QTableWidget::item:selected {\n    background-color: #3498DB;\n    color: black;");
    
    // 应用更新后的样式表
    setStyleSheet(updatedStyleSheet);
    
    qDebug() << "充值成功后已将页面白色字体修改为黑色";
}